package com.huike.business.mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.huike.business.domain.TbBusiness;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.core.parameters.P;

/**
 * 商机Mapper接口
 * @date 2021-04-25
 */
public interface TbBusinessMapper {
    /**
     * 查询商机
     *
     * @param id 商机ID
     * @return 商机
     */
    public TbBusiness selectTbBusinessById(Long id);


    /**
     * 查询商机列表
     *
     * @param tbBusiness 商机
     * @return 商机集合
     */
    public List<TbBusiness> selectTbBusinessList(TbBusiness tbBusiness);


    public List<TbBusiness> selectTbBusinessPool(TbBusiness tbBusiness);


    /**
     * 新增商机
     *
     * @param tbBusiness 商机
     * @return 结果
     */
    public int insertTbBusiness(TbBusiness tbBusiness);

    /**
     * 修改商机
     *
     * @param tbBusiness 商机
     * @return 结果
     */
    public int updateTbBusiness(TbBusiness tbBusiness);

    /**
     * 删除商机
     *
     * @param id 商机ID
     * @return 结果
     */
    public int deleteTbBusinessById(Long id);

    /**
     * 批量删除商机
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteTbBusinessByIds(Long[] ids);

    /**
     * 重新分配商机的状态
     * @param id
     * @param status
     * @return
     */
    public int resetNextTimeAndStatus(@Param("id") Long id, @Param("status") String status);


    public int setTransfer(@Param("id") Long id, @Param("status") String status);

    public void updateBusinessEndTimeById(@Param("id")Long id, @Param("endTime")Date endTime);

    /**
     * 查询所有商机
     * @param beginCreateTime
     * @param endCreateTime
     */
    @Select("select count(id) from tb_business where create_time BETWEEN #{beginCreateTime} AND #{endCreateTime}")
    Integer selectAllBusiness(@Param("beginCreateTime") String beginCreateTime,@Param("endCreateTime") String endCreateTime);

    /**
     * 成功商机转部门集合
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Select("    SELECT   d.create_by,count(c.id) as num,d.dept_name as deptName  from tb_contract c  LEFT JOIN sys_dept d" +
            "    on c.dept_id =d.dept_id where DATE_FORMAT(c.create_time,'%y-%m-%d') BETWEEN  DATE_FORMAT(#{beginCreateTime},'%y-%m-%d') AND DATE_FORMAT(#{endCreateTime},'%y-%m-%d')" +
            "    GROUP BY c.create_by" +
            "    ORDER BY num desc" +
            "    LIMIT 10")
    List<Map<String,Object>> selectAllContractByDept(@Param("beginCreateTime") String beginCreateTime,@Param("endCreateTime") String endCreateTime);

    /**
     * 所有线程
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Select("select count(id) from tb_clue where create_time BETWEEN #{beginCreateTime} AND #{endCreateTime}")
    Integer selectAllClues(@Param("beginCreateTime") String beginCreateTime,@Param("endCreateTime") String endCreateTime);

    /**
     * 成功线索转换集合
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    List<Map<String, Object>> selectAllBusinessByDept(@Param("beginCreateTime") String beginCreateTime,@Param("endCreateTime") String endCreateTime);

    /**
     * 删除分配记录
     * @param businessId
     */
    @Delete("delete  from tb_assign_record where assign_id=#{businessId}")
    void deleteRecord(Long businessId);


}
